package com.example.signsystem.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.signsystem.common.enums.UnderStandEnum;
import com.example.signsystem.entity.dto.QuestionUnderstandDTO;
import com.example.signsystem.entity.parm.QuestionParam;
import com.example.signsystem.entity.parm.QuestionUnderstandParam;
import com.example.signsystem.entity.po.QuestionPO;
import com.example.signsystem.entity.po.QuestionUnderstandPO;
import com.example.signsystem.entity.po.StudentsPO;
import com.example.signsystem.mapper.QuestionMapper;
import com.example.signsystem.mapper.QuestionUnderstandMapper;
import com.example.signsystem.mapper.StudentsMapper;
import com.example.signsystem.service.IQuestionService;
import com.example.signsystem.util.PageResultFactory;
import com.qcby.framework.common.pojo.PageParam;
import com.qcby.framework.common.pojo.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, QuestionPO> implements IQuestionService {

    @Resource
    private StudentsMapper studentsMapper;

    @Resource
    private QuestionUnderstandMapper questionUnderstandMapper;

    @Override
    public Boolean questionPosting(QuestionParam questionParam) {
        QuestionPO questionPO = BeanUtil.copyProperties(questionParam, QuestionPO.class);
        int insert=this.baseMapper.insert(questionPO);
        return insert>0;
    }

    @Override
    public PageResult<QuestionPO> checkQuestion(PageParam pageParam) {
        IPage page = new Page(pageParam.getPageNo(),pageParam.getPageSize());
        PageResult<QuestionPO> result = PageResultFactory.createPageResult(this.baseMapper.selectPage(page, null));
        return result ;
    }

    @Override
    public Boolean questionAnswer(QuestionUnderstandParam questionUnderstandParam) {
        QuestionUnderstandPO questionUnderstandPO=BeanUtil.copyProperties(questionUnderstandParam,QuestionUnderstandPO.class);
        /**通过用户姓名获得userId**/
        StudentsPO studentsPO = studentsMapper.selectOne(new LambdaQueryWrapper<StudentsPO>()
                .eq(StudentsPO::getName, questionUnderstandParam.getName()));
        /**判断userId是否存在，存在则更新听懂状态为已听懂**/
        Long count = questionUnderstandMapper.selectCount(new LambdaQueryWrapper<QuestionUnderstandPO>()
                .eq(QuestionUnderstandPO::getUserId, studentsPO.getId())
                .eq(QuestionUnderstandPO::getQuestionId,questionUnderstandParam.getQuestionId()));
        if(count>0){
            /**更新understand为1**/
            UpdateWrapper<QuestionUnderstandPO> updateWrapper = new UpdateWrapper<QuestionUnderstandPO>()
                    .eq("user_id",studentsPO.getId());
            questionUnderstandPO.setUnderstand(UnderStandEnum.YES.getCode());
            int temp=questionUnderstandMapper.update(questionUnderstandPO,updateWrapper);
            return temp>0;
        }
        /**构造返回体**/
        questionUnderstandPO.setUserId(studentsPO.getId());
        return questionUnderstandMapper.insert(questionUnderstandPO)>0;
    }

    @Override
    public QuestionUnderstandDTO questionUnderstand(Long questionId) {
        /**听懂的人**/
        List<QuestionUnderstandPO> questionUnderstandPOS = questionUnderstandMapper
                .selectList(new LambdaQueryWrapper<QuestionUnderstandPO>()
                        .eq(QuestionUnderstandPO::getUnderstand, UnderStandEnum.YES.getCode())
                .eq(QuestionUnderstandPO::getQuestionId,questionId));

        int count1= CollectionUtil.size(questionUnderstandPOS);
        /**未听懂的**/
        List<QuestionUnderstandPO> questionNotUnderstandPOS = questionUnderstandMapper
                .selectList(new LambdaQueryWrapper<QuestionUnderstandPO>()
                        .eq(QuestionUnderstandPO::getUnderstand, UnderStandEnum.NO.getCode())
                        .eq(QuestionUnderstandPO::getQuestionId,questionId));
        int count2= CollectionUtil.size(questionNotUnderstandPOS);
        /**返回体**/
        QuestionUnderstandDTO questionUnderstandDTO=new QuestionUnderstandDTO();
        questionUnderstandDTO.setUnderstandNum(count1);
        questionUnderstandDTO.setNotUnderstandNum(count2);
        questionUnderstandDTO.setUnderstand(questionUnderstandPOS);
        questionUnderstandDTO.setNotUnderstand(questionNotUnderstandPOS);
        return questionUnderstandDTO;
    }
}
